﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace VarianceReportWinUI
{
    public partial class frmWorkQueue : DockContentEx
    {
        public frmWorkQueue()
        {
            InitializeComponent();
            mListViewSortManager = new ListViewSorter.ListViewSortManager(lstWorkQueue, new Type[] { typeof(ListViewSorter.ListViewTextSort), typeof(ListViewSorter.ListViewTextSort), typeof(ListViewSorter.ListViewDateSort), typeof(ListViewSorter.ListViewDateSort) });

        }

        private ListViewSorter.ListViewSortManager mListViewSortManager = null;


        public override void UpdateViewer()
        {
            mListViewSortManager.Suspend();

            lstWorkQueue.Items.Clear();

            foreach (var item in this.uiHandler.workQueue.WorkItemList)
            {
                var lvItem = lstWorkQueue.Items.Add(item.Name);
                lvItem.SubItems.Add(item.Description);
                lvItem.SubItems.Add(item.Created.StringValueOrEmpty());
                lvItem.SubItems.Add(item.Updated.StringValueOrEmpty());
                lvItem.Tag = item.Key;
            }

            if (this.uiHandler.workQueue.WorkItemList.Count > 0)
            {
                for (int i = 0; i < lstWorkQueue.Columns.Count; i++)
                {
                    lstWorkQueue.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }
            mListViewSortManager.Resume();

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            NewWorkItem();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenSelected();
        }
 
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstWorkQueue.SelectedItems.Count > 0)
            {
                var selected = lstWorkQueue.SelectedItems[0];

                var workItem = uiHandler.workQueue.WorkItemList.First(t => t.Key == (Guid)selected.Tag);
                uiHandler.CloseWorkItem(workItem);

                uiHandler.workQueue.WorkItemList.Remove(workItem);
                UpdateViewer();
            }
        }

        private void lstWorkQueue_DoubleClick(object sender, EventArgs e)
        {
            OpenSelected();
        }

        public void OpenSelected()
        {
            if (lstWorkQueue.SelectedItems.Count > 0)
            {
                var selected = lstWorkQueue.SelectedItems[0];

                uiHandler.ViewWorkItem(uiHandler.workQueue.WorkItemList.First(t => t.Key == (Guid)selected.Tag));
            }
        }

        public void NewWorkItem()
        {
            if (dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                var tNewWorkItem = new WorkItem(dlgOpenFile.FileName);
                uiHandler.workQueue.WorkItemList.Add(tNewWorkItem);
                UpdateViewer();
                uiHandler.ViewWorkItem(tNewWorkItem);
            }
        }

        private void frmWorkQueue_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            uiHandler.ShowOrHideQueue();
        }

    }
}
